/**
*  
* @Function 系统消息Controller
* @ProjectName credit-data-xypj 
* @PackageName com.py.credit.controller    
* @FileName SysMsgController.java    
* @Date 2018年12月17日上午11:11:34
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/
package com.py.credit.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.GatewayUserEntity;
import com.py.credit.entity.SysMsgEntity;
import com.py.credit.service.GatewayAuthenticationService;
import com.py.credit.service.MsgService;
import com.py.framework.core.common.Page;
import com.py.framework.core.controller.BaseController;
import com.py.framework.core.helper.HttpServletHelper;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * @Function 系统消息Controller
 * @ClassName SysMsgController
 * @Date 2018年12月17日 上午11:11:34
 * @Author Min DongXv
 * 
 */
@Api(tags = "xypj-msg", description = "信用评价消息")
@RestController
@RequestMapping("/xypj/gateway/msg")
public class SysMsgController extends BaseController {

	@Autowired
	private MsgService msgService;

	@Autowired
	private GatewayAuthenticationService gatewayAuthenticationService;

	@ApiOperation(value = "分页查询消息列表", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "msgId", value = "消息ID", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "int", name = "msgStatus", value = "消息状态", required = false) })
	@RequestMapping("/queryMsgRecords.json")
	public RestServiceResult<List<Map<String, Object>>> queryMsgRecords(Page page, HttpServletRequest request,
			@RequestParam(value = "msgId", defaultValue = "", required = false) Long msgId,
			@RequestParam(value = "msgStatus", defaultValue = "", required = false) Integer msgStatus) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
		try {
			// 用户信息
			GatewayUserEntity user = gatewayAuthenticationService
					.getCurrentUserInfo(HttpServletHelper.getUserXypjGatewayToken(request));
			if (null == user || user.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("SysMsgController#queryMsgRecords", "未获取到当前用户信息！");
				return result;
			}
			List<Map<String, Object>> data = msgService.queryMsgRecordByPage(msgId, user.getId(), msgStatus, page);
			result.setData(data);
			result.setCount(page.getTotalCount());
			return result;
		} catch (Exception e) {
			result.setData(Collections.emptyList());
			result.setCount(0);
			ExceptionLogger.error("SysMsgController#queryMsgRecords", "分页查询消息异常！ERROR：" + e.getMessage(), e);
			return result;
		}
	}

	@ApiOperation(value = "查询消息TOP5", httpMethod = "POST", produces = "application/json")
	@RequestMapping("/queryTopMsg.json")
	public RestServiceResult<List<Map<String, Object>>> queryTopMsg(HttpServletRequest request) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
		try {
			// 用户信息
			GatewayUserEntity user = gatewayAuthenticationService
					.getCurrentUserInfo(HttpServletHelper.getUserXypjGatewayToken(request));
			if (null == user || user.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("SysMsgController#queryTopMsg", "未获取到当前用户信息！");
				return result;
			}
			List<Map<String, Object>> data = msgService.queryMsgRecord(user.getId());
			result.setData(data);
			return result;
		} catch (Exception e) {
			result.setData(Collections.emptyList());
			result.setCount(0);
			ExceptionLogger.error("SysMsgController#queryTopMsg", "查询消息TOP5！ERROR：" + e.getMessage(), e);
			return result;
		}
	}

	@ApiOperation(value = "消息详情", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "long", name = "id", value = "消息ID", required = true) })
	@RequestMapping("/findOneMsg.json")
	public RestServiceResult<SysMsgEntity> findOneMsg(
			@RequestParam(value = "id", defaultValue = "", required = true) Long id) {
		RestServiceResult<SysMsgEntity> result = new RestServiceResult<>();
		try {
			result.setData(msgService.findOneMsg(id));
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("查询消息详情异常！");
			ExceptionLogger.error("SysMsgController#findOneMsg", "查询消息详情异常！ERROE:" + e.getMessage(), e);
			return result;
		}
	}

	@ApiOperation(value = "新增消息", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "SysMsgEntity", name = "entity", value = "消息实体", required = true) })
	@RequestMapping("/addMsgInfo.json")
	public RestServiceResult<Object> addMsgInfo(@RequestBody SysMsgEntity entity) {
		RestServiceResult<Object> result = new RestServiceResult<>();
		try {
			msgService.addMsgInfo(entity);
			return result;
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("新增消息异常！");
			ExceptionLogger.error("SysMsgController#addMsgInfo", "新增消息异常！ERROE:" + e.getMessage(), e);
			return result;
		}
	}

	@ApiOperation(value = "删除消息", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "ids", value = "消息ID", required = true) })
	@RequestMapping("/delMsg.json")
	public RestServiceResult<Object> delMsg(
			@RequestParam(value = "ids", defaultValue = "", required = true) String ids) {
		RestServiceResult<Object> result = new RestServiceResult<>();
		try {
			List<Long> list = null;
			if (StringUtils.isNotBlank(ids)) {
				String[] array = ids.split(",");
				if (null != array && array.length > 0) {
					list = new ArrayList<>(array.length);
					for (int i = 0; i < array.length; i++) {
						list.add(Long.parseLong(array[i]));
					}
				}
			}
			msgService.delMsg(list);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("删除消息异常！");
			ExceptionLogger.error("SysMsgController#delMsg", "删除消息异常！ERROE:" + e.getMessage(), e);
			return result;
		}
		return result;
	}

	@ApiOperation(value = "更新消息状态", httpMethod = "POST", produces = "application/json")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "id", value = "消息ID", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "status", value = "消息状态", required = true) })
	@RequestMapping("/updateMsgStatus.json")
	public RestServiceResult<Object> updateMsgStatus(
			@RequestParam(value = "id", defaultValue = "", required = true) Long id,
			@RequestParam(value = "status", defaultValue = "", required = true) Integer status) {
		RestServiceResult<Object> result = new RestServiceResult<>();
		try {
			msgService.updateMsgStatus(status, id);
		} catch (Exception e) {
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("更新消息状态！");
			ExceptionLogger.error("SysMsgController#updateMsgStatus", "更新消息状态！ERROE:" + e.getMessage(), e);
			return result;
		}
		return result;
	}
}